package jk.fox.exchange.netty;

import io.netty.channel.ChannelFuture;
import jk.fox.common.RpcURIUtils;
import jk.fox.exchange.ExchangeChannel;
import lombok.Getter;
import lombok.Setter;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

/**
 *
 * @author cuichao
 * @Description: NettyChannel
 * @Date: create in 2020/5/17 11:46
 */
@Getter
@Setter
public class NettyChannel implements ExchangeChannel<ChannelFuture> {

    private ChannelFuture future;
    private String key;
    private SocketAddress remoteAddress;

    public NettyChannel(ChannelFuture future){
        this.future = future;
        this.remoteAddress = future.channel().remoteAddress();
        InetSocketAddress address = (InetSocketAddress) this.remoteAddress;
        key = RpcURIUtils.getAddress(address);
    }


    @Override
    public String key() {
        return key;
    }

    @Override
    public ChannelFuture channel() {
        return future;
    }


    @Override
    public boolean isAlive() {
        return future != null && future.channel().isActive();
    }


    @Override
    public SocketAddress remoteAddress() {
        return remoteAddress;
    }





}
